package com.wanghangcloud.common.utils;

import com.wanghangcloud.common.constant.SessionConstants;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.HttpHeaders;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 用户工具类 从header中获取登录用户信息
 */
public class UserUtil {

    /**
     * 获取用户id,统一获取处理
     *
     * @return
     */
    public static String getUserId() {
        HttpServletRequest request = getRequest();
        if (request == null) {
            return null;
        }
        return request.getHeader("user_id");
    }

    public static String getUsername() {
        HttpServletRequest request = getRequest();
        if (request == null) {
            return null;
        }
        String userName = request.getHeader("user_name");
        if (userName == null) {
            return null;
        }
        try {
            return URLDecoder.decode(userName, StandardCharsets.UTF_8.toString());
        } catch (UnsupportedEncodingException e) {
            return userName;
        }
    }

    public static String getDeptId() {
        HttpServletRequest request = getRequest();
        if (request == null) {
            return null;
        }
        return request.getHeader("dept_id");
    }

    public static String getMobile() {
        HttpServletRequest request = getRequest();
        if (request == null) {
            return null;
        }
        return request.getHeader("mobile");
    }

    public static Integer getDataScope() {
        HttpServletRequest request = getRequest();
        if (request == null) {
            return null;
        }
        String data_scope = request.getHeader("data_scope");
        if (!StringUtils.hasText(data_scope)){
            return null;
        }
        return Integer.valueOf(data_scope);
    }

    public static Set<String> getAuthorities() {
        HttpServletRequest request = getRequest();
        if (request == null) {
            return null;
        }
        // 获取userId
        String authorities = request.getHeader("authorities");
        if (!StringUtils.hasText(authorities)){
            return new HashSet<>();
        }
        return Stream.of(authorities.split(",")).collect(Collectors.toSet());
    }

    public static String getUserType() {
        HttpServletRequest request = getRequest();
        if (request == null) {
            return null;
        }
        return request.getHeader("user_type");
    }

    public static String getName() {
        HttpServletRequest request = getRequest();
        if (request == null) {
            return null;
        }
        String name = request.getHeader("name");
        if (name == null) {
            return null;
        }
        try {
            return URLDecoder.decode(name,StandardCharsets.UTF_8.toString());
        } catch (UnsupportedEncodingException e) {
           return name;
        }
    }

    /**
     * 获取appId,统一获取处理
     *
     * @return
     */
    public static String getAppId() {
        HttpServletRequest request = getRequest();
        if (request == null) {
            return null;
        }
        return request.getHeader("appId");
    }

    /**
     * 获取appCode,统一获取处理
     *
     * @return appCode，如果无法获取则返回空
     */
    public static String getAppCode() {
        HttpServletRequest request = getRequest();
        if (request == null) {
            return null;
        }
        return request.getHeader("appCode");
    }


    /**
     * 获取请求中的 Token
     *
     * @return Token 字符串
     */
    public static String getToken() {
        HttpServletRequest request = getRequest();
        if (request == null) {
            return null;
        }
        return request.getHeader(HttpHeaders.AUTHORIZATION);
    }

    /**
     * 获取登录用户
     */
    public static Object getUserPrincipal() {
        HttpServletRequest request = getRequest();
        if (request == null) {
            return null;
        }
        return request.getAttribute(SessionConstants.ATTR_USER_PRINCIPAL);
    }

    /**
     * 缓存登录用户
     */
    public static void setUserPrincipal(Object principal) {
        HttpServletRequest request = getRequest();
        if (request == null) {
            return;
        }
        request.setAttribute(SessionConstants.ATTR_USER_PRINCIPAL, principal);
    }

    /**
     * request可能为空
     * 1.非HTTP请求上下文
     * 2.异步处理
     * 3.手动调用
     * @return
     */
    public static HttpServletRequest getRequest(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            return null;
        }
        return requestAttributes.getRequest();
    }
}
